﻿<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" MasterPageFile="~/Views/Shared/Site.Master" %>

<asp:Content runat="server" ID="PageTitle" ContentPlaceHolderID="PageTitle">
    <title>易得桌面</title>
</asp:Content>
<asp:Content runat="server" ID="PageStyles" ContentPlaceHolderID="PageStyles">
    <style type="text/css">        
        
        #menu_finishWorkitem
        {
            width: 160px;
        }
        .divExp
        {
            border-bottom: 1px solid #F5F5F5;
            margin-top: 15px;
        }
        
        .workitem-finished
        {
            margin-bottom: 4px;
            font-size: 14px;
            border-bottom: 1px solid #F5F5F5;
            word-break: break-all;
            word-wrap: break-word;
        }
        .workitem-unfinished
        {
            margin-bottom: 4px;
            font-size: 12px;
            color: #848484;
            border-bottom: 1px solid #F5F5F5;
            word-break: break-all;
            word-wrap: break-word;
        }
        .target-finished
        {
            -webkit-border-radius: 3px;
            width: 40px;
            color: #FFFFFF;
            background-color: #3BA365;
            font-size: 12px;
            text-align: center;
            padding-left: 3px;
            float: left;
        }
        .target-unfinished
        {
            -webkit-border-radius: 3px;
            width: 40px;
            background-color: #65A5F2;
            padding-left: 3px;
            float: left;
        }
        .title
        {
            max-width: 190px;
            margin-left: 4px;
            padding-left: 3px;
            display: inline-block;
        }
        .txtDisabled
        {
            color: #848484;
        }
        .workitemInfo
        {
            color: #848484;
            padding-right: 3px;
            display: inline-block;
            float: right;
        }
        .noWorkitem
        {
            text-align: center;
        }
        .imgAdd
        {
            background: #fff9dd url(./../Content/images/plus.png) no-repeat;
        }

        .freecontent-content a:hover
        {
            padding-left: 0px;
        }

        
        #txtDailyWork
        {
            font-size: 16px;
            width: 460px;
        }
        #txtDailyWork:hover
        {
            background-color: #FFFFAA;
        }
    </style>
</asp:Content>
<asp:Content runat="server" ID="PageScripts" ContentPlaceHolderID="PageScripts">
    <script type="text/javascript">
    //************* URL常量
    var URL_FINISHED_WORKITEM_LIST = "/project/OwnDailyFinishedWorkitems";
    var URL_UNFINISHED_WORKITEM_LIST = "/project/OwnUnfinishedWorkitems";
    var URL_FINISH_WORKITEM = '/project/DoFinishWorkitem';
    var URL_UPDATE_DAILYWORK ='/project/DoUpdateDailyWork';
    var URL_DO_ADD_FINISHED_WORKITEM='/project/DoAddFinishedWorkItem'

    var Service = {};
    function InitService(B) {
        Service.GetFinishedWorkitemList = $.AsycnMethod(URL_FINISHED_WORKITEM_LIST);
        Service.GetUnfinishedWorkitemList = $.AsycnMethod(URL_UNFINISHED_WORKITEM_LIST);
        Service.DoUpdateDailyWork = $.AsycnMethod(URL_UPDATE_DAILYWORK,"Post");
    }
    //************* 全局变量

    var g_Var = {
        "time": null,
        "isEdited": false
    };

var StaticResource = {};

//************* 常量定义


//************* Accessor访问器
var EzAccessor = {};

function CreateAccessor() {
    if (ezmock.IS_MOCKED) {
        EzAccessor = CreateMockEzAccessor();

    } else {
        EzAccessor = CreateRealEzAccessor();
    }
}


function CreateMockEzAccessor() {
    //模拟实现服务器交互方法

    var mockEzAccessor = {

};

return mockEzAccessor;
}

function CreateRealEzAccessor() {
    //实现服务器交互方法
    var realEzAccessor = {
        "getTime" : function() {
             return <%= WebJsonExtension.ToJson(Model.Time) %>;  
        },
        "getIsFuture": function() {
            return <%= WebJsonExtension.ToJson(Model.IsFuture) %>;  
        },
        "getIsDisplayUnfinishedWorkitems": function() {
            return <%= WebJsonExtension.ToJson(Model.IsDisplayUnfinishedWorkitems) %>;  
        },
         "getDailyWork": function() {
            return <%= WebJsonExtension.ToJson(Model.DailyWork) %>;  
        }
    };
    return realEzAccessor;
};



function  bindWorkitemsFinished(workitems){
    if(workitems.length > 0){
        $('#divMyWorkitems').empty();
        for(var key in workitems) {
            var workitemDom = $('<div class="workitem-finished"></div>');
            //完成标志
            var target = '<div class="target-finished"><span>✔</span></div>';
            //任务标题
            var titleStr = '<div  class="title"><span>{title}</span></div>';
            titleStr =  titleStr.replace("{title}", workitems[key].Title);
                                
            //任务信息
            var infoStr = '<div class="workitemInfo"><span>{CreatorName}&nbsp;创建于&nbsp;{createOn}&nbsp;工时:&nbsp;{estimatePoint}小时</span></div>';
            infoStr  = infoStr.replace("{CreatorName}", workitems[key].CreatorName)
                              .replace("{createOn}", Date.fromEpoch(workitems[key].CreateOn).toNiceTime())
                              .replace("{estimatePoint}", workitems[key].Points);
            workitemDom.append(target)
                       .append(titleStr)
                       .append(infoStr);
            $('#divMyWorkitems').append(workitemDom); 
            //idData
            $(workitemDom).data("workitemId", workitems[key].Id);
            //freecontent
           

        }    
    }else {
            $('#divMyWorkitems').append('<p class="noWorkitem">暂时没有完成的任务</p>'); 
    }       
}



function  bindWorkitemsUnfinished(workitems){
    $('#divNeedToDo').empty();
    if(workitems.length > 0){
        for(var key in workitems) {
            var workitemDom = $('<div class="workitem-unfinished"></div>');
            //未完成标志
            var target = '<div class="target-unfinished"><span>&nbsp;</span></div>';
            //任务标题
            var titleStr = '<div class="title"><span >{title}</span></div>';
            titleStr =  titleStr.replace("{title}", workitems[key].Title);
                                
            //任务信息
            var infoStr = '<div  class="workitemInfo"><span>{CreatorName} {createOn} 创建 &nbsp;&nbsp; 预计工时:&nbsp;{estimatePoint}小时</span></div>';
            infoStr  = infoStr.replace("{CreatorName}", workitems[key].CreatorName)
                              .replace("{createOn}", Date.fromEpoch(workitems[key].CreateOn).toNiceTime())
                              .replace("{estimatePoint}", workitems[key].Points);
            workitemDom.append(target)
                       .append(titleStr)
                       .append(infoStr);
            $('#divNeedToDo').append(workitemDom); 
            //idData
            $(workitemDom).data("workitemId", workitems[key].Id);
            //freecontent
            
        }    
    }else {
        $('#divNeedToDo').append('<p class="noWorkitem">暂时未没有完成的任务</p>'); 
    }   
    
    var setTRCss = function (tr, color , mouseStatus){
        $(tr).css({
            "background-color": color,
            "cursor" : mouseStatus,
        });    
    };
    $('.target-unfinished').each( function () {

        var _self = $(this); 
        var backgroundColor = _self.css('background-color');
        //hover
        _self.hover(function() {
            setTRCss(_self, '#3BA365', 'pointer');
                
        }, function() {
            setTRCss(_self, backgroundColor, 'default');
        });

        _self.click(function() {
        
            $('#menu_finishWorkitem').contextPanel({
                parent: this,
                contextPanelClose: function () {
                    $('#txtHours').clearFields();
                    }
            });

            $('#txtHours').defaultText('所用工时');
            $('#txtHours').select();

            //完成任务
            $('#btnFinish').click(function  (){
                var workitemId = _self.parent().data("workitemId"); 
                var points = $('#txtHours').val();
                if(workitemId == null) return false; 
                finishWorkitem(workitemId, points, function () {
                    $(document).ajaxBusy('end');
                    
                    $('#menu_finishWorkitem').contextPanel("close");
                        
                    Service.GetFinishedWorkitemList(bindWorkitemsFinished,{dateTime:g_Var["time"]});
                    Service.GetUnfinishedWorkitemList(bindWorkitemsUnfinished);        
                });
            }); 
            
           
        });
    });    
}


function finishWorkitem (id, points, callback) {
    $(document).ajaxBusy('begin');
    $.post(URL_FINISH_WORKITEM, 
        {
          'workitemId': id,
          'points': points
        },
         function (e) {
            callback(e);
        });
}

function bindDailyTitle (date) {
    var dateEpoch = Date.fromEpoch(date).toFullDate();
    $('#txtDailyTitle').html(dateEpoch + " " + "工作日志");
}


function bindDailyWork(dailyWork){
    if(dailyWork.htmlEncode().trim() !=  ""){
        $('#txtDailyWork').html(dailyWork);
    }   
    else {
        $('#txtDailyWork').html('单击此处添加日志');
    } 
}

//初始化页面
function initPage() {
    CreateAccessor();
    InitService();

    EzTipsManager.addTipBox('#tip_Remind', 'tipRemind');
    EzTipsManager.showTipBox('tipRemind');

    var isFuture = EzAccessor.getIsFuture();
    if (!isFuture){
        //当日日志
        var dailyWork = EzAccessor.getDailyWork();
        bindDailyWork(dailyWork);
        //当日完成任务
        g_Var["time"] = EzAccessor.getTime();
        bindDailyTitle(g_Var["time"]);
        Service.GetFinishedWorkitemList(bindWorkitemsFinished,{dateTime:g_Var["time"]});    
        //当日未完成任务
        var isDisplayUnfinishedWorkitems = EzAccessor.getIsDisplayUnfinishedWorkitems();
        if(isDisplayUnfinishedWorkitems) {
            Service.GetUnfinishedWorkitemList(bindWorkitemsUnfinished);
            
            $('#nextDay').hide();
            $('#divAddWorkItem').show(); 
        
           
        }
        
    }else{
        //todo: 
    }
}


function getIsDailyWorkEmpty(){
    var isDailyWorkEmpty = false;
    if($('#txtDailyWork').html() == "单击此处添加日志"){
        isDailyWorkEmpty = true;    
    }

    return isDailyWorkEmpty;
}

//初始化事件
function initPageEvent() {
     $('#preDay').click(function () {
            var date = Date.fromEpoch(g_Var["time"]);
            var perDay = new Date(
                date.getFullYear(),
                date.getMonth(),
                date.getDate() - 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "/project/DailyWork?time="
                           + perDay;

        });


        $('#nextDay').click(function () {
            var date = Date.fromEpoch(g_Var["time"]);
            var nextDay = new Date(
                date.getFullYear(),
                date.getMonth(),
                date.getDate() + 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "/project/DailyWork?time=" + nextDay;

        });

        $('#addFinishedWorkitem').click( function () {
            if (WorkitemCreator) {
                    WorkitemCreator.Show(true,URL_DO_ADD_FINISHED_WORKITEM);
                    WorkitemCreator.SubmitSuccessCallback = function () {
                        Service.GetFinishedWorkitemList(bindWorkitemsFinished);
                    }
                }
        });

        $('#txtDailyWork').click(function (){
            $(this).hide();
            $('.divExp').show();

            if(getIsDailyWorkEmpty() == false) {
                $('#template').val($('#txtDailyWork').html().htmlEncode().trim());    
            }
            $('#template').select();
        });

        $('#btnCancel').click(function (){
            $('#txtDailyWork').show();
            $('.divExp').hide();
            $('#template').clearFields();
        });

        $('#btnSumbit').click(function (){
            $('#txtDailyWork').show();
            $('.divExp').hide();
            //提交日志
            dailyWorkContent = $('#template').val().htmlEncode().trim();
            Service.DoUpdateDailyWork(
                function(e){
                    $.displayStatusInfo(e);
                    if(dailyWorkContent == ""){
                        $('#txtDailyWork').html("单击此处添加日志");
                    }else{
                        $('#txtDailyWork').html(dailyWorkContent);
                    }
                    $('#template').clearFields();
                }
               ,{datetime:g_Var['time'],dailyWorkContent:dailyWorkContent});
        });

    }

$(function () {
    initPage();
    initPageEvent();
});
    </script>
</asp:Content>
<asp:Content ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
    <div id="status-bar">
        <% Html.RenderPartial("_navigateTo"); %><a href="/project/dailyWorkList" class="head">工作日志列表</a></div>
</asp:Content>
<asp:Content runat="server" ID="Page" ContentPlaceHolderID="PageContent">
    <div id="page-head">
        <img alt="diaryWork" height="128" src="../../Content/icons/diaryWork.png" style="width: 48px;
            height: 48px;" width="128" />
        <span id="txtDailyTitle"></span>
        <div style="margin-top: 5px; min-height: 20px;">
            <div style="float: left;">
                <a id="preDay">前一天</a>
            </div>
            <div style="float: right;">
                <a id="nextDay">后一天</a>
            </div>
        </div>
    </div>
    <div id="page-body">
        <div class="content">
            <div id="divMyWorkitems" style="margin-top: 10px;">
            </div>
            <div id="divAddWorkItem" style="margin-top: 10px; border-bottom: 1px solid #F5F5F5;
                display: none;">
                <span id="addFinishedWorkitem" class="imgAdd"><a style="font-size: 16px; margin-left: 30px;"
                    class="highlight">添加已完成的任务</a></span>
            </div>
            <div id="divNewWorkItem">
                <% Html.RenderPartial("_workitemCreator"); %>
            </div>
            <div id="divDailyWork" style="margin-top: 10px; border-bottom: 1px solid #F5F5F5;">
                <p id="txtDailyWork">
                </p>
            </div>
            <div class="divExp" style="margin-top: 10px; display: none;">
                <textarea id="template" cols="58" rows="5" style="margin-bottom: 10px;"></textarea>
                <div style="margin-bottom: 10px;">
                    <input id="btnSumbit" type="button" value="提交日志" />&nbsp;或者&nbsp;<a id="btnCancel">取消</a>
                </div>
            </div>
            <div id="divNeedToDo" style="margin-top: 20px;">
            </div>
        </div>
    </div>
    <div class="page-submit">
    </div>
</asp:Content>
<asp:Content runat="server" ID="PageShortcut" ContentPlaceHolderID="PageShortcut">
</asp:Content>
<asp:Content runat="server" ID="PageFoot" ContentPlaceHolderID="PageFoot">
    <div id="menu_finishWorkitem" style="display: none;">
        <div style="margin-top: 4px; margin-left: 4px;">
            <input id="txtHours" class="text" style="width: 100px;" />
            &nbsp;小时
        </div>
        <p>
            <input id="btnFinish" type="button" value="完成"/>
        </p>
    </div>
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
        </p>
    </div>
</asp:Content>
